package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    FeeKeyMapper feeKeyMapper;
    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    CityInfoMapper cityInfoMapper;
    @Autowired
    DistrictInfoMapper districtInfoMapper;


    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo detailVo = new ApartmentDetailVo();

        //获取公寓主信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo,detailVo);
        //获取图片信息
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<>(GraphInfo.class).eq(GraphInfo::getItemType, 1).eq(GraphInfo::getItemId,id));
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        detailVo.setGraphVoList(graphVoList);

        //获取配套
        List<FacilityInfo> facilityInfoList = apartmentFacilityMapper.selectFacilityInfo(id);
        detailVo.setFacilityInfoList(facilityInfoList);

        //获取标签
        List<LabelInfo> labelInfoList = apartmentLabelMapper.selectLabelInfo(id);
        detailVo.setLabelInfoList(labelInfoList);

        //根据公寓id获取最小租金
        RoomInfo roomInfo = roomInfoMapper.selectOne(new LambdaQueryWrapper<>(RoomInfo.class)
                .eq(RoomInfo::getApartmentId, id)
                .orderByAsc(RoomInfo::getRent)
                .last(" limit 1")
        );
        detailVo.setMinRent(roomInfo.getRent());

        return detailVo;
    }
}




